#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#if defined(TEST_TARGET_coroutine)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/coroutine.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "coroutine.h"
#endif

#define MUTI_THREAD_NUM 2
#if (MUTI_THREAD_NUM <= 0)
#error "MUTI_THREAD_NUM must be greater than 0"
#endif

#ifdef _WIN32
#include <windows.h>
uint64_t GetTimerUsec(void)
{
    LARGE_INTEGER li;
    LARGE_INTEGER frequency;
    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&li);
    return (uint64_t)li.QuadPart * 1000000 / frequency.QuadPart;
}
#else
#include <sys/time.h>
uint64_t GetTimerUsec(void)
{
    struct timeval mstime;
    uint64_t us = 0;
    gettimeofday(&mstime, NULL);
    us = mstime.tv_sec * 1000000 + mstime.tv_usec;
    return us;
}
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "coroutine suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

static CoEvent gEvent = COEVENT_STATIC_VALUE;
static CoTask_t gTickCoroutine = NULL;
static CoTask_t gTockCoroutine = NULL;
static uint8_t gStackTick[1024 * 10];
static uint8_t gStackTock[1024 * 10];
static CoTimer_t gTimer0Handle = NULL;
static CoTimer_t gTimer1Handle = NULL;

static void *tick(void *arg)
{
    while (1)
    {
        CoTask_Wait(.ms=1000);
        printf("tick-running...\n");
        CoEvent_Notify(&gEvent, 0x02);
    }
}

static void *tock(void *arg)
{
    while (1)
    {
        uint32_t evs = CoTask_Wait(.pEvent=&gEvent);
        if (evs & 0x01)
        {
            printf("event 0x01 triggered...\n");
        }
        if (evs & 0x02)
        {
            printf("event 0x02 triggered...\n");
        }
    }
}

static void timer0(void)
{
    printf("timer0-running...\n");
}

static void timer1(void)
{
    printf("timer1-running... %d\n", CoScheduler_CurLoad(CoTimer_SchedulerId()));
    // CoEvent_Notify(&gEvent, 0x01 | 0x02);
}

/* Testing begins here */
void *createTask(void *arg)
{
    // CoTask_Create(tick, g_StackTick, sizeof(g_StackTick), 0);
    // CoTask_Create(tock, g_StackTock, sizeof(g_StackTock), 0);
    CoTask_Create(tick);
    CoTask_Create(tock);
    // timerHandle0 = CoTimer_CreateMs(timer0_entry, 100);
    // timerHandle1 = CoTimer_CreateMs(timer1_entry, 1000);
}

#if (MUTI_THREAD_NUM == 1)
void test_singleThread(void)
{
    CoScheduler_Init(0, GetTimerUsec, 1000);
    createTask(NULL);
    CoScheduler_Start(0);
}
#else
static pthread_t threads[MUTI_THREAD_NUM];
static pthread_t testThread;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static void thread_lock(void)
{
    pthread_mutex_lock(&mutex);
}

static void thread_unlock(void)
{
    pthread_mutex_unlock(&mutex);
}

static void *muti_thread_entry(void *arg)
{
    CoScheduler_Start(*(int *)arg);
}

void test_mutiThread(void)
{
    int threadId[MUTI_THREAD_NUM];
    for (int i = 0; i < MUTI_THREAD_NUM; i++) 
    {
        CoScheduler_Init(i, GetTimerUsec, 1000, .lock=thread_lock, .unlock=thread_unlock);
    }
    createTask(NULL);
    for (int i = 0; i < MUTI_THREAD_NUM; i++) 
    {
        threadId[i] = i;
        pthread_create(&threads[i], NULL, muti_thread_entry, &threadId[i]);

        /* You can bind thread to specific core */
        /* So that each CoScheduler can run on different core */
    }
    for (int i = 0; i < MUTI_THREAD_NUM; i++)
    {
        pthread_join(threads[i], NULL);
    }
}
#endif

static void test_base(void)
{
#if (MUTI_THREAD_NUM == 1)
    test_singleThread();
#else
    test_mutiThread();
#endif
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: coroutine [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        ...\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"
    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        // Add others opt here
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("coroutine version %d.%d.%d\r\n", COROUTINE_V_MAJOR, COROUTINE_V_MINOR, COROUTINE_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            #if defined(TEST_TARGET_coroutine)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
    }
    else  
    {
        test_base();
    }

#if 0
    if (1 == command_optind) // no opt
    {
        
    }

    for (int index = command_optind; index < argc; index++) 
    {
        if (!strcmp(argv[index], "base"))
        {
            test_base();
        }
    }

    if (1 == argc)
    {
        test_base();
    }
#endif 
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_coroutine)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_coroutine(void)
{
    command_export("coroutine", test);
}
init_export_app(test_coroutine);
#endif 
